﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BadHabit.Factory.Windows {

    /// <summary>
    /// Interaction logic for BacklogItemDetail.xaml
    /// </summary>
    public partial class BacklogItemDetail : BadHabit.WPFTools.ShapedWindow {

        BindingListCollectionView collectionView;

        object[] oldItemValues;

        protected override void OnClosed(EventArgs e) {
            base.OnClosed(e);
            if (Client.FactoryMainWindow != null)
                Client.FactoryMainWindow.IsPopUpOpened = false;
        }

       
        public BacklogItemDetail(Data.ProjectDataSet.ProjectRow project, BindingListCollectionView collectionView) {
            InitializeComponent();

            artifactListBox.Project = project;
            Bind(collectionView);            
            
          
        }


        private void Bind(BindingListCollectionView collectionView) {
            
            this.collectionView = collectionView;
            
            DataContext = collectionView.CurrentItem;
            System.Data.DataRowView rowView = collectionView.CurrentItem as System.Data.DataRowView;
                        
            artifactListBox.BacklogItemUId = rowView["BacklogItemUId"].ToString();

            oldItemValues = rowView.Row.ItemArray;
        }


        
        private void Save_Executed(object sender, ExecutedRoutedEventArgs e) {
            this.Close();
        }

        private void Undo_Executed(object sender, ExecutedRoutedEventArgs e) {            
            System.Data.DataRowView itemView = DataContext as System.Data.DataRowView;

            if (!Helper.IsArrayEqual(itemView.Row.ItemArray,oldItemValues)) {

                if (collectionView != null)
                    collectionView.EditItem(itemView);

                itemView.Row.ItemArray = oldItemValues;

                if (collectionView != null)
                    collectionView.CommitEdit();
                collectionView.MoveCurrentTo(itemView);

            }
            this.Close();
        }

        private void ShapedWindow_Loaded(object sender, RoutedEventArgs e) {
            Client.FactoryMainWindow.IsPopUpOpened = true;
            artifactListBox.LoadArtifacts();      
        }

        private void verifiedButton_Click(object sender, RoutedEventArgs e) {
            Data.ProjectDataSet.BacklogItemRow item = Helper.GetRowFromRowView<Data.ProjectDataSet.BacklogItemRow>(DataContext);
            if (item == null)
                return;

            MessageBoxResult d = Windows.Error.ShowDialog(
                Properties.Resources.Verify_item_confirm_message, Properties.Resources.Verify_Item, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
            if (d == MessageBoxResult.No)
                return;

            item.VerifiedDate = DateTime.Now;
            item.VerifiedBy = Client.User.Identity.Name;
        }

        private void validadedButton_Click(object sender, RoutedEventArgs e) {
            Data.ProjectDataSet.BacklogItemRow item = Helper.GetRowFromRowView<Data.ProjectDataSet.BacklogItemRow>(DataContext);
            if (item == null)
                return;

            MessageBoxResult d = Windows.Error.ShowDialog(Properties.Resources.Validate_item_confirm_message, Properties.Resources.Validate_Item, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
            if (d == MessageBoxResult.No)
                return;

            item.ValidatedDate = DateTime.Now;
            item.ValidatedBy = Client.User.Identity.Name;
        }

        private void previousButton_Click(object sender, RoutedEventArgs e) {

            GetFocusOut();

            if (collectionView.CurrentPosition > 0) 
                collectionView.MoveCurrentToPrevious();
            else
                collectionView.MoveCurrentToLast();
            Bind(collectionView);
        }

        private void nextButton_Click(object sender, RoutedEventArgs e) {

            GetFocusOut();
         
            if (collectionView.CurrentPosition < collectionView.Count - 1)
                collectionView.MoveCurrentToNext();
            else
                collectionView.MoveCurrentToFirst();

            Bind(collectionView);
            
        }

        private void GetFocusOut() {
            // YEH..THIS LINES ARE UGLY.
            // just do that to move the focus from the control being edited
            // so the value is comited
            IInputElement focused = Keyboard.FocusedElement;
            Keyboard.Focus(acceptedDateTextBox);
            Keyboard.Focus(focused);

        }
    }
}
